import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/can_comment_model.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_easy_loading.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';

class GameUserComment extends StatefulWidget {
  final String id;
  GameUserComment({required this.id, Key? key}) : super(key: key);
  @override
  GameUserCommentState createState() => GameUserCommentState();
}

class GameUserCommentState extends State<GameUserComment> {
  List<CanCommentModel> _commentRows = [];
  CanCommentModel? _selectModel;
  // bool isCanAdd = true;
  int periodiTime = 0;
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 400.w + ScreenUtil().bottomBarHeight,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(15), topRight: Radius.circular(15))),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.only(left: 15.w),
            height: 48.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  S.current.game_selectComment,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: ThemeColor.colorBlack100,
                  ),
                ),
                GestureDetector(
                    onTap: ClickUtils.debounce(() {
                      Navigator.pop(context);
                    }),
                    child: SizedBox(
                      width: 44.w,
                      height: 44.w,
                      child: Center(
                        child: Image.asset(
                          'assets/icons/game_comment_close.png',
                          width: 14,
                          height: 14,
                        ),
                      ),
                    ))
              ],
            ),
          ),
          Expanded(
              child: Container(
                  width: 345.w,
                  // color: Colors.red,
                  // padding: EdgeInsets.symmetric(horizontal: 15.w),
                  child: SingleChildScrollView(
                    child: Wrap(
                      spacing: 10.w,
                      runSpacing: 16.w,
                      crossAxisAlignment: WrapCrossAlignment.start,
                      alignment: WrapAlignment.start,
                      children: _commentRows.map((e) {
                        return GestureDetector(
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 12, vertical: 6),
                            decoration: BoxDecoration(
                                color: e == _selectModel
                                    ? ThemeColor.primaryColor
                                    : Colors.white,
                                borderRadius: BorderRadius.circular(4),
                                border: Border.all(
                                    width: 0.5,
                                    color: e == _selectModel
                                        ? Colors.transparent
                                        : Color(0xff979797))),
                            child: Text(
                              e.comment ?? '',
                              style: TextStyle(
                                fontSize: 14,
                                color: ThemeColor.colorBlack100,
                              ),
                            ),
                          ),
                          onTap: ClickUtils.debounce(() {
                            if (e == _selectModel) {
                              _selectModel = null;
                            } else {
                              _selectModel = e;
                            }
                            if (mounted) {
                              setState(() {});
                            }
                          }),
                        );
                      }).toList(),
                    ),
                  ))),
          Container(
            color: Color(0xFFE4E4E4),
            height: 1,
          ),
          GestureDetector(
            onTap: ClickUtils.debounce(() async {
              if (_selectModel != null && periodiTime <= 0) {
                loading(() async {
                  bool addSucceess = await API.shared.game
                      .addUserComments(widget.id, [_selectModel!]);
                  Navigator.pop(context, addSucceess);
                });
              }
            }),
            child: Container(
              height: 40,
              margin: EdgeInsets.only(
                  left: 15.w,
                  right: 15.w,
                  top: 5.w,
                  bottom: ScreenUtil().bottomBarHeight + 5.w),
              decoration: BoxDecoration(
                color: (_selectModel == null || periodiTime > 0)
                    ? ThemeColor.primaryColor.withOpacity(0.2)
                    : ThemeColor.primaryColor,
                borderRadius: BorderRadius.circular(8),
              ),
              child: Center(
                child: Text(
                  S.current.service_send +
                      (periodiTime > 0 ? "($periodiTime)" : ''),
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: (_selectModel == null || periodiTime > 0)
                        ? ThemeColor.colorBlack100.withOpacity(0.2)
                        : ThemeColor.colorBlack100,
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  updatePeriod(int index) {
    periodiTime = index;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void initState() {
    super.initState();
    API.shared.game.canUserComments(widget.id).then((value) {
      DLog(value);
      _commentRows = value;
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(GameUserComment oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
